package com.example.baiduai.ai.service.impl;

import com.example.baiduai.ai.dto.*;
import com.example.baiduai.ai.service.AiImageService;
import com.example.baiduai.ai.utils.AiUtils;
import com.example.baiduai.ai.vo.*;
import com.example.baiduai.utils.GsonUtils;
import com.example.baiduai.utils.HttpUtils;
import com.example.baiduai.utils.JsonResult;
import com.google.common.collect.Maps;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AilmageServiceimpl implements AiImageService {

    @Override
    public JsonResult aiImagesClassify(List<MultipartFile> files, Integer top_num) {

        ArrayList<Object> imagesList = new ArrayList<>();

        for (MultipartFile file : files) {
            String base64Images = fliebase64(file);
            HashMap<String, String> maps = Maps.newHashMap();
            maps.put("image",base64Images);
            String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/aiImagesClassify", maps, AiUtils.tokenParmsMap());
            AiImagesClassifyVo aiImagesClassifyVo = GsonUtils.toBean(s, AiImagesClassifyVo.class);
            imagesList.add(aiImagesClassifyVo);
        }
        return JsonResult.success(imagesList);
    }


    @SneakyThrows
    public String fliebase64(MultipartFile mFile){
        Base64Utils.encode(mFile.getBytes());
        BASE64Encoder base64Encoder =new BASE64Encoder();
        String base64EncoderImg = base64Encoder.encode(mFile.getBytes());
        return base64EncoderImg;
    }

    /**
     * 数据集创建API
     * @param createDataSetDto
     * @return
     */
    @Override
    public JsonResult createDataSet(CreateDataSetDto createDataSetDto) {

        String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/easydl/dataset/create", createDataSetDto, AiUtils.tokenParmsMap());
        CreateDataSetVo createDataSetVo = GsonUtils.toBean(s, CreateDataSetVo.class);
        if(ObjectUtils.isNotEmpty(createDataSetVo) && ObjectUtils.isEmpty(createDataSetVo.getError_code()) ) return JsonResult.success(createDataSetVo);
        return JsonResult.fail(createDataSetVo);
    }


    /**
     * 数据集列表API
     * @param datasetListDto
     * @return
     */
    @Override
    public JsonResult dataSetList( DatasetListDto datasetListDto) {
        String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/easydl/dataset/list", datasetListDto, AiUtils.tokenParmsMap());
        DataSetListVo dataSetListVo = GsonUtils.toBean(s, DataSetListVo.class);
        if(ObjectUtils.isNotEmpty(dataSetListVo) && ObjectUtils.isEmpty(dataSetListVo.getError_code()) ) return JsonResult.success(dataSetListVo);
        return JsonResult.fail(dataSetListVo);
    }


    /**
     * 分类（标签）列表API
     * @param lableDto
     * @return
     */
    @Override
    public JsonResult labeList(LableDto lableDto) {
        String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/easydl/label/list", lableDto, AiUtils.tokenParmsMap());
        LabelVo labelVo = GsonUtils.toBean(s, LabelVo.class);
        if(ObjectUtils.isNotEmpty(labelVo) && ObjectUtils.isEmpty(labelVo.getError_code()) ) return JsonResult.success(labelVo);
        return JsonResult.fail(labelVo);
    }


    /**
     * 添加数据API
     * @param datasetAdd
     * @return
     */
    @Override
    public JsonResult datasetAdd(DatasetAdd datasetAdd) {
        String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/easydl/dataset/addentity", datasetAdd, AiUtils.tokenParmsMap());
        DatasetVo datasetAddVo = GsonUtils.toBean(s, DatasetVo.class);
        if(ObjectUtils.isNotEmpty(datasetAddVo) && ObjectUtils.isEmpty(datasetAddVo.getError_code()) ) return JsonResult.success(datasetAddVo);
        return JsonResult.fail(datasetAddVo);
    }

    /**
     * 数据集删除API
     * @param datasetDelete
     * @return
     */
    @Override
    public JsonResult datasetDelete(DatasetDelete datasetDelete) {
        String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/easydl/dataset/addentity", datasetDelete, AiUtils.tokenParmsMap());
        DatasetVo datasetDelVo = GsonUtils.toBean(s, DatasetVo.class);
        if(ObjectUtils.isNotEmpty(datasetDelVo) && ObjectUtils.isEmpty(datasetDelVo.getError_code()) ) return JsonResult.success(datasetDelVo);
        return JsonResult.fail(datasetDelVo);
    }

    @Override
    public JsonResult labelDelete(LabelDeleteDto labelDelete) {
        String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/easydl/label/delete", labelDelete, AiUtils.tokenParmsMap());
        DatasetVo lableDelVo = GsonUtils.toBean(s, DatasetVo.class);
        if(ObjectUtils.isNotEmpty(lableDelVo) && ObjectUtils.isEmpty(lableDelVo.getError_code()) ) return JsonResult.success(lableDelVo);
        return JsonResult.fail(lableDelVo);
    }

    @Override
    public JsonResult payDetection(List<MultipartFile> files, String threshold) {
        ArrayList<Object> imagesList = new ArrayList<>();

        for (MultipartFile file : files) {
            String base64Images = fliebase64(file);
            HashMap<String, String> maps = Maps.newHashMap();
            maps.put("image",base64Images);
            maps.put("threshold",threshold);
            String s = HttpUtils.doPostJson("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/payDetection", maps, AiUtils.tokenParmsMap());
            AiImagesObjectDetectionVo aiImagesClassifyVo = GsonUtils.toBean(s, AiImagesObjectDetectionVo.class);
            imagesList.add(aiImagesClassifyVo);
        }
        return JsonResult.success(imagesList);
    }
}
